package org.hive.sequence
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import flash.events.Event;
	import org.hive.state.State;
	import org.hive.events.SequenceEvent;
	import org.hive.sequence.Sequence;
	
	public class ActionSequence extends Sequence
	{
		/**
		* @private
		*/
		//private var __active:Boolean = false;

		/**
		*
		*/
		private var __nextState:State;

		/**
		*
		*/
		private var __switchState:Boolean = false;

		/**
		*
		*/
		private var __nextSequence:ActionSequence;

		/**
		*
		*/
		private var __complete:Boolean = false;
		
		/**
		*
		* @param	name
		* @param	sequences
		*/
		public function ActionSequence( name:String, sequences:Array=null )
		{
			super( name, sequences );
		}
		
		/**
		*
		* @param	startIndex
		* @param	endIndex
		* @return
		*/
		public override function slice( startIndex:Number=0, endIndex:Number=16777215 ):*
		{
			var seq:ActionSequence 		= new ActionSequence( __name, __REPOSITORY.slice( startIndex, endIndex ) );
			
			seq.completeTriggerName 	= __completeEventName;
			seq.completeTriggerSource 	= __completeEventSource;

			
			seq.nextState 				= __nextState;
			seq.switchState             = __switchState;
			seq.nextSequence            = __nextSequence;

			return seq;
		}

		/**
		*
		* @param	value
		* @return
		*/
		public override function set complete( value:Boolean ):void
		{
			super.complete = value;

			if( __complete && !__active ){
				var event:SequenceEvent = new SequenceEvent( SequenceEvent.ON_COMPLETE, __nextSequence );
				dispatchEvent( event );
			}
		}
		
		/**
		*
		* @param	e
		* @return
		*/
		internal override function handleSequenceEnd( e:Event ):void
		{
			super.handleSequenceEnd( e );

			//trace('ActionSequence :: handleSequenceEnd : ' + e);

			var event:SequenceEvent = new SequenceEvent( SequenceEvent.ON_COMPLETE, __nextSequence, __switchState, __nextState, e );
			dispatchEvent( event );
		}
		//______________________________________________________________________________ GETTERS AND SETTERS
		
			
		public function set nextState( state:State )
		{
			__nextState = state;
		}
		
		public function get nextState( ):State
		{
			return __nextState;
		}

		public function set switchState( contentSwitch:Boolean )
		{
			__switchState = contentSwitch;
		}
		
		public function get switchState( ):Boolean
		{
			return __switchState;
		}
		
		public function set nextSequence( sequence:ActionSequence )
		{
			__nextSequence = sequence;
		}
		
		public function get nextSequence( ):ActionSequence
		{
			return __nextSequence;
		}
		
		/**
		* @private
		*/
		public override function toString():String
		{
			var returnString:String = "[ActionSequence \n \t" + __name ;
			returnString += "\t\t" + __REPOSITORY.toString( ) + " ]";
			
			return returnString;
		}
	}
}
